﻿using System;
using System.Collections.Generic;
using CkSoftware.GroupMe.Sdk.Core;
using CkSoftware.GroupMe.Sdk.Core.Dao;
using CkSoftware.GroupMe.Sdk.Messaging.Commands;
using CkSoftware.GroupMe.Sdk.Messaging.Dao;
using CkSoftware.GroupMe.Sdk.Messaging.Enums;

namespace CkSoftware.GroupMe.Sdk.Messaging
{
	/// <summary>
	/// Manager class for direct messages. Handles loading, creating, liking and unliking direct messages.
	/// </summary>
	public class GroupMeDirectMessages : GroupMeMessages
	{
		private readonly string _memberId;

		/// <summary>
		/// Instantiates a new <see cref="GroupMeDirectMessages" /> object.
		/// </summary>
		/// <param name="webApiAccess">The <see cref="IWebApiAccess" /> to use for API communication.</param>
		/// <param name="memberId">The opponent member id with which the direct-message handling should be performed. Attention: needs to be the UserId property, not the Id property of a GroupMemberData object.</param>
		public GroupMeDirectMessages(IWebApiAccess webApiAccess, string memberId) : base(webApiAccess, memberId)
		{
			_memberId = memberId;
		}
		
		/// <summary>
		/// Instantiates a new CreateMessageCommand for direct messages.
		/// </summary>
		/// <param name="sourceGuid">The source-guid to use in the direct-message.</param>
		/// <param name="text">The text to use in the direct-message.</param>
		/// <param name="attachments">The attachments to use in the direct-message.</param>
		/// <returns>The correctly constructed IWebApiCommand.</returns>
		protected override IWebApiCommand<GroupMeApiResponse<GroupMeMessageData>> GetCreateMessageCommand(string sourceGuid,
			string text, IEnumerable<GroupMeAttachmentBase> attachments)
		{
			var command = new DirectMessagesCreateCommand(_memberId, sourceGuid);

			if (!string.IsNullOrEmpty(text))
			{
				command.WithText(text);
			}

			if (attachments != null)
			{
				foreach (GroupMeAttachmentBase attachment in attachments)
				{
					command.WithAttachment(attachment);
				}
			}

			if (!command.IsParametersValid())
			{
				throw new ArgumentException("Please add at least either a text or an attachment.");
			}

			return command;
		}

		/// <summary>
		/// Instantiates a new IndexCommand for direct messages.
		/// </summary>
		/// <param name="direction">The direction of the IndexCommand.</param>
		/// <returns>The correctly constructed IWebApiCommand.</returns>
		protected override IWebApiCommand<GroupMeApiResponse<MessagesIndexData>> GetLoadMessagesCommandForDirection(
			MessageLoadDirection direction)
		{
			DirectMessagesIndexParameters parameters = GetParametersForDirection(direction);
			return new DirectMessagesIndexCommand(parameters);
		}

		private DirectMessagesIndexParameters GetParametersForDirection(MessageLoadDirection direction)
		{
			if (!InitialLoadDone)
			{
				return new DirectMessagesIndexParameters(_memberId);
			}

			switch (direction)
			{
				case MessageLoadDirection.Backward:
					return new DirectMessagesIndexParameters(_memberId, OldestMessageId);
				case MessageLoadDirection.Forward:
					return new DirectMessagesIndexParameters(_memberId, sinceId: NewestMessageId);
				default:
					return new DirectMessagesIndexParameters(_memberId);
			}
		}
	}
}